<?php
declare (strict_types = 1);

namespace app\tools;

use app\exception\LogicException;
use app\facade\CurlFacade;
use Exception;
use think\App;
use think\facade\Log;

final class BaiDuTool
{
    private $ak;

    private $url = "http://api.map.baidu.com";

    /**
     * WechatTool constructor.
     * @throws Exception
     */
    public function __construct()
    {
        $this->ak = config('map.ak');
    }

    /**
     * 地点检索2.0
     *
     * @param $params
     * @return mixed
     * @throws LogicException
     */
    public function circlePlaceV2Search($params)
    {
        $url = "{$this->url}/place/v2/search?output=json&ret_coordtype=gcj02ll&ak={$this->ak}&scope=2";

        $page = isset($params['page']) ? $params['page'] : 1;
        $page_size = isset($params['page_size']) ? $params['page_size'] : 20;

        $url .= '&query=' . $params['query'];
        $url .= '&tag=' . $params['tag'];
        $url .= '&radius=' . $params['radius'];
        $url .= '&filter=sort_name:distance|sort_rule:1';
        $url .= '&filter=sort_name:distance|sort_rule:1';
        $url .= '&page_num=' . $page;
        $url .= '&page_size=' . $page_size;

        if (isset($params['location'])) {
            $url .= '&location=' . $params['location'];
        } else {
            throw new LogicException(lang('communal.map api params error'));
        }

        /*
         * 坐标的类型，目前支持的坐标类型包括：
         * bd09ll（百度经纬度坐标）、bd09mc（百度米制坐标）
         * gcj02ll（国测局经纬度坐标，仅限中国）、wgs84ll（ GPS经纬度）
         */
        if (isset($params['coordtype'])) {
            $url .= '&coord_type=' . $params['coordtype'];
        } else {
            $url .= '&coord_type=2';
        }

        $response = json_decode(CurlFacade::curlGET($url), true);

        if (0 != $response['status']) {
            throw new LogicException($response['msg']);
        }

        return $response['results'];
    }


    /**
     * 地理编码
     *
     * @param $params
     * @return mixed
     * @throws LogicException
     */
    public function geocoding($params)
    {
        $url = "{$this->url}/geocoding/v3/?output=json&ak={$this->ak}";

        if (isset($params['address'])) {
            $url .= '&address=' . trim(str_replace(" ", '', (string)$params['address']));
        } else {
            throw new LogicException(lang('communal.map api params error'));
        }

        /*
         * 坐标的类型，目前支持的坐标类型包括：
         * bd09ll（百度经纬度坐标）、bd09mc（百度米制坐标）
         * gcj02ll（国测局经纬度坐标，仅限中国）、wgs84ll（ GPS经纬度）
         */
        if (isset($params['coordtype'])) {
            $url .= '&ret_coordtype=' . $params['coordtype'];
        } else {
            $url .= '&ret_coordtype=gcj02ll';
        }

        $response = json_decode(CurlFacade::curlGET($url), true);

        if (0 != $response['status']) {
            throw new LogicException($response['msg']);
        }

        return $response['result'];
    }

    /**
     * 逆地理编码, 逆地理编码境外POI服务为开放平台高级服务，需申请开通权限后才能访问服务
     *
     * @param $params
     * @return mixed
     * @throws LogicException
     */
    public function reverseGeocoding($params)
    {
        $url = "{$this->url}/reverse_geocoding/v3/?ak={$this->ak}&output=json";

        if (isset($params['location'])) {
            $url .= '&location=' . $params['location'];
        } else {
            throw new LogicException(lang('communal.map api params error'));
        }

        /*
         * 坐标的类型，目前支持的坐标类型包括：
         * bd09ll（百度经纬度坐标）、bd09mc（百度米制坐标）
         * gcj02ll（国测局经纬度坐标，仅限中国）、wgs84ll（ GPS经纬度）
         */
        if (isset($params['coordtype'])) {
            $url .= '&coordtype=' . $params['coordtype'];
        } else {
            $url .= '&coordtype=gcj02ll';
        }

        $response = json_decode(CurlFacade::curlGET($url), true);

        if (0 != $response['status']) {
            throw new LogicException($response['msg']);
        }

        return $response['result'];
    }
}
